//
//  PywUnbundAccountSuccessController.m
//  PywSdk
//
//  Created by smallwave on 16/8/31.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywUnbundAccountSuccessController.h"
#import "UILabel+Pyw.h"
#import "UIButton+Pyw.h"
#import "PywMasonry.h"
#import "PywPlatformMain.h"
#import "PywLogoutManger.h"
#import "PywSdk+login.h"
#import "PywCPNotifications.h"
#import "PywUserViewController.h"
#import "PywAccountManagerViewController.h"
#import "PywUploadAccountLoginManager.h"

@interface PywUnbundAccountSuccessController ()

@property (nonatomic, strong) UILabel   *accountInfoLabel;  // 账号信息
@property (nonatomic, strong) UIButton  *backBtn;           // 返回按钮
@property (nonatomic, strong) UILabel   *reminderLabel;     // 温馨提示

@property (nonatomic, copy) NSString    *account;           // 游戏账号
@property (nonatomic, copy) NSString    *loginPwd;          // 登录密码

@end

@implementation PywUnbundAccountSuccessController

#pragma mark - Init
- (instancetype)initWithGameAccount:(NSString *)account
                           LoginPwd:(NSString *)loginPwd{
    self = [super init];
    if (self) {
        self.account = account;
        self.loginPwd = loginPwd;
    }
    return self;
}

#pragma mark - Lift Cycle Method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
}

#pragma mark - prepareUI
- (void)setUp{
    self.title = @"解绑成功";
    self.navigationItem.rightBarButtonItem = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.interactivePopGestureRecognizer.enabled = NO; // 禁止系统的手势返回
    [self leftBarButtonItem];
    
    [self addSubviewAndLayoutSubviews];
}
- (void)addSubviewAndLayoutSubviews{
    [self.view addSubview:self.accountInfoLabel];
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.reminderLabel];
    
    __weak typeof(self) wSelf = self;
    [self.accountInfoLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.view.mas_top).offset(80);
    }];
    [self.backBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.accountInfoLabel.mas_bottom).offset(40);
        make.width.equalTo(wSelf.view.mas_width).multipliedBy(0.85);
        make.height.equalTo(@40);
    }];
    [self.reminderLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.backBtn.mas_bottom).offset(20);
        make.width.equalTo(wSelf.backBtn.mas_width);
    }];
    
}
- (void)leftBarButtonItem{
    UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    back.backgroundColor = [UIColor clearColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = item;
}
#pragma mark - Action
// 返回
- (void)backAction{
    [self.view endEditing:YES];
    
    for (UIViewController *gameAccountVC in self.navigationController.viewControllers) {
        if ([[PywPlatformMain sharedPlatformMain].account isEqualToString:self.account]) {
            [self unBindLoginAccount:self.account];
            break;
        }else {
            if ([gameAccountVC isKindOfClass:[PywAccountManagerViewController class]]) {
                [self.navigationController popToViewController:gameAccountVC animated:YES];
                break;
            }
        }
    }
}
/**
 *  解绑当前账号
 */
- (void)unBindLoginAccount:(NSString *)account{
    
    if ([[PywPlatformMain sharedPlatformMain].account isEqualToString:account]) {
        
        // 注销账号，保存当前登录账号结束时间
        [[PywUploadAccountLoginManager manager] saveAccountLoginEndTime];
        
        [[PywLogoutManger manger] clearLogin];
        
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
            
            // 发送注销完成的通知
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            [dict setValue:@"注销成功" forKey:@"info"];
            [dict setValue:[NSString stringWithFormat:@"%d",PYW_LOGOUT_STATE_NORMAL] forKey:@"result"];
            [[NSNotificationCenter defaultCenter] postNotificationName:kPywCPLogoutNotification object:nil userInfo:dict];
        }];
    }
    
}
#pragma mark - Lazy
- (UILabel *)accountInfoLabel{
    if (!_accountInfoLabel) {
        _accountInfoLabel = [UILabel labelWithText:nil
                                     textAlignment:NSTextAlignmentLeft
                                          fontSize:14
                                         textColor:UIColorFromRGB(0x333333)];
        
        NSString *attributedStr = [NSString stringWithFormat:@"游戏账号：%@\n\n登录密码：%@",self.account,self.loginPwd];
        NSMutableAttributedString *attributedMessage = [[NSMutableAttributedString alloc] initWithString:attributedStr];
        // 设置不同的显示颜色
        [attributedMessage addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xFFBC00) range:NSMakeRange(5, self.account.length)];
        [attributedMessage addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xFFBC00) range:NSMakeRange(12 + self.account.length, self.loginPwd.length)];
        _accountInfoLabel.attributedText = attributedMessage;
        [_accountInfoLabel sizeToFit];
    }
    return _accountInfoLabel;
}
- (UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithBackgroundColor:UIColorFromRGB(0xFFBC00)
                                          cornerRadius:20
                                            titleColor:[UIColor whiteColor]
                                    titleLabelFontSize:18
                                                 title:@"返回"];
        
        [_backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        [_backBtn setAccessibilityIdentifier:@"bunphone_back"];
    }
    return _backBtn;
}
- (UILabel *)reminderLabel{
    if (!_reminderLabel) {
        _reminderLabel = [UILabel labelWithText:@"温馨提示：该账号和密码仅当前游戏可用"
                                    textAlignment:NSTextAlignmentCenter
                                         fontSize:12
                                        textColor:UIColorFromRGB(0x333333)];
    }
    return _reminderLabel;
}

@end
